package com.sun.controller;

import com.github.pagehelper.PageInfo;
import com.sun.entities.Emp;
import com.sun.service.EmpService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author hugh.sun
 * @date 2021/11/15 0:50
 */
@Api
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    @PostMapping("/emp")
    public String addEmp(Emp emp){
        empService.add(emp);
        return "200 ok";
    }

    @GetMapping("/emp/{id}")
    public Object getEmpById(@PathVariable("id") Integer id){
        /*模拟多用户进行操作 创建线程池*/
        ExecutorService es = Executors.newFixedThreadPool(200);
        for(int i = 0; i < 500; i++){
            es.submit(()->{
                empService.getEmpById(id);
            });
        }
        Object emp = empService.getEmpById(id);
        return emp;
    }
    /*分页查询*/
    @GetMapping("/emp/name")
    public PageInfo selectUserByname(Integer pageIndex,Integer pageSize) {
        return empService.selectUserByname(pageIndex,pageSize);
    }

}
